# VideoOSDScreen - This screen is used for video playback OSD
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import gobject
import clutter
from clutter import cluttercairo

from frontend.gui.widgets.arrow_texture import ArrowTexture

from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

class VideoOSDScreen(Screen):
    """
    VideoOSDScreen
    
    This screen is displayed when video is being watched. Usually this screen
    doesn't have any visible elements. User actions such as pause and rewind
    are displayed as on-screen-graphics.
    """
    
    def __init__(self, ui, theme, player):
        """Initialize screen"""
        self.__ui = ui
        self.__theme = theme
        self.__player = player
        self.__group = clutter.Group()
        
        self.__create_aspect_ratio_textures()
        
        # Pause
        # FIXME: Remove these and display timeline when paused
        self.pause_label = clutter.Label()
        self.pause_label.set_font_name(self.__theme.getFont() + ' 48')
        self.pause_label.set_text("Pause")
        self.pause_label.set_color(clutter.Color(255,255,255,255))
        self.pause_label.set_position(50,50)
        
        self.pause_bg = clutter.Rectangle()
        self.pause_bg.set_color(clutter.Color(10,37,69,192))
        self.pause_bg.set_border_color(clutter.Color(255,255,255,255))
        self.pause_bg.set_border_width(5)
        self.pause_bg.set_position(30,45)
        self.pause_bg.set_size(160,65)
        
        self.__group.add(self.pause_bg)
        self.__group.add(self.pause_label)
        self.__ui.getStage().add(self.__group)
    
    def __create_video_timeline(self):
        """
        Create video timeline widget. This widget displayes current position
        of the stream and video length
        """
        
    def __create_volume_overlay(self):
        """
        Create volume overlay widget. Displayed when user changes volume
        """
        
    def __create_aspect_ratio_textures(self):
        """
        Create textures that which are displayed when user changes aspect ratio.
        """
        #FIXME: We could use Cairo instead of pixmap graphics
        pixbuf1 = gtk.gdk.pixbuf_new_from_file(self.__theme.getImage("native_aspect_ratio"))
        pixbuf2 = gtk.gdk.pixbuf_new_from_file(self.__theme.getImage("widescreen_aspect_ratio"))
        pixbuf3 = gtk.gdk.pixbuf_new_from_file(self.__theme.getImage("zoom_aspect_ratio"))
        pixbuf4 = gtk.gdk.pixbuf_new_from_file(self.__theme.getImage("compromise_aspect_ratio"))
        texture_1 = clutter.Texture()
        texture_2 = clutter.Texture()
        texture_3 = clutter.Texture()
        texture_4 = clutter.Texture()
        texture_1.set_pixbuf(pixbuf1)
        texture_2.set_pixbuf(pixbuf2)
        texture_3.set_pixbuf(pixbuf3)
        texture_4.set_pixbuf(pixbuf4)
        self.__group.add(texture_1)
        self.__group.add(texture_2)
        self.__group.add(texture_3)
        self.__group.add(texture_4)
        self.__aspect_textures = [texture_1, texture_2, texture_3, texture_4]
        self.__timeout_key = None # This is used when canceling timeouts
        for texture in self.__aspect_textures:
            texture.set_position((self.__ui.STAGE_WIDTH - texture.get_width()) / 2, 
                                 int(self.__ui.STAGE_HEIGHT / 1.5))
    
    def __hide_aspect_ratio_logo(self, number):
        """
        Hide aspect ratio texture. This is a callback function and
        shouldn't be called directly.
        """
        self.__aspect_textures[number].hide()
        self.__timeout_key = None
        return False
    
    def __display_aspect_ratio_logo(self, number):
        """
        Display aspect ratio logo on screen when ratio is changed.
        @param number: Number of the aspect ratio
        """
        if self.__timeout_key is not None:
            gobject.source_remove(self.__timeout_key) 
        for texture in self.__aspect_textures:
            texture.hide()
        self.__aspect_textures[number].show()
        self.__timeout_key = gobject.timeout_add(2000, 
                                                 self.__hide_aspect_ratio_logo, 
                                                 number)
    
    def get_group(self):
        """Return all actors of the screen."""
        return self.__group

    def get_type(self):
        """Return screen type."""
        return Screen.OSD
    
    def get_name(self):
        """Return screen name (human readble)"""
        return "VideoOSD"
    
    def handle_key_press_event(self, event):   
        """ Handle key pressed event. This can be transmitted to the widgets."""
        # Toggle pause/play
        if event.keyval == clutter.keysyms.p:
            if self.__player.is_playing():
                self.pause_bg.hide()
                self.pause_label.hide()
            else:
                self.pause_bg.show()
                self.pause_label.show()
        
        # Stop
        elif event.keyval == clutter.keysyms.s:
            print "CREATE VIDEO STOPPED SCREEN HERE"
            
        # Video aspect ratio changed. Display aspect ratio logo on screen
        elif event.keyval == clutter.keysyms._1:
            self.__display_aspect_ratio_logo(0)
        elif event.keyval == clutter.keysyms._2:
            self.__display_aspect_ratio_logo(1)
        elif event.keyval == clutter.keysyms._3:
            self.__display_aspect_ratio_logo(2)
        elif event.keyval == clutter.keysyms._4:
            self.__display_aspect_ratio_logo(3)
